import {createRouter, createWebHistory,createWebHashHistory} from 'vue-router'
import Login from '../views/Login.vue'
// import User from '../views/admin/User.vue'
import Layout from "../Layout/Layout";
import Register from "../views/Register";
// import Blog from "../views/Blog";
// import Comment from "../views/Comment";
// import Message from "../views/Message";
// import Tag from "../views/Tag";
// import Type from "../views/Type";
// import ForeHome from "../views/ForeHome";
// import ForeIndex from "../views/ForeIndex";
// import ForeBlog from "../views/ForeBlog";
// import ForePerson from "../views/ForePerson";
// import ForePublish from "../views/ForePublish";
// import ChatRoom from "../views/ChatRoom"

// let userJson = sessionStorage.getItem("user");

// if (userJson == null || userJson.role === "ROLE_ADMIN") {
//     redirectPath = '/admin/user'
// }

const routes = [
    {
        path: '/admin',
        name: 'Layout',
        component: Layout,
        redirect: '/admin/index',
        children: [
            {
                path: '/admin/index',
                name: 'index',
                component: () => import('@/views/admin/Index.vue')
            },
            {
                path: '/admin/user',
                name: 'user',
                component: () => import('@/views/admin/User.vue')
            },
            {
                path: '/admin/blog',
                name: 'Blog',
                component: () => import('@/views/admin/Blog.vue')
            },
            {
                path: '/admin/comment',
                name: 'Comment',
                component: () => import('@/views/admin/Comment.vue')
            },
            {
                path: '/admin/message',
                name: 'Message',
                component: () => import('@/views/admin/Message.vue')
            },
            {
                path: '/admin/type',
                name: 'Type',
                component: () => import('@/views/admin/Type.vue')
            },
            {
                path: '/admin/tag',
                name: 'Tag',
                component: () => import('@/views/admin/Tag.vue')
            }
        ]
    },
    {
        path: '/login',
        name: 'Login',
        component: Login
    },
    {
        path: '/register',
        name: 'Register',
        component: Register
    },
    {
        path: '/',
        name: 'ForeHome',
        component: () => import('@/views/blog/ForeHome.vue'),
        children: [
            {
                path: '/index',
                name: 'ForeIndex',
                component: () => import('@/views/blog/ForeIndex.vue')
            },
            {
                path: '/blog/:id',
                name: 'ForeBlog',
                component: () => import('@/views/blog/ForeBlog.vue'),
                props: true
            },
            {
                path: '/chat',
                name: 'ChatRoom',
                component: () => import('@/views/blog/ChatRoom.vue'),
                props: true
            },
            {
                path: '/guestbook',
                name: 'guestbook',
                component: () => import('@/views/blog/guestbook.vue')
            },
            {
                path:'/person/:id',
                name:'ForePerson',
                component: () => import('@/views/blog/ForePerson.vue')
            },
            {
                path:'/publish',
                name:'ForePublish',
                // props:true,
                // props: { newsletterPopup: false },
                component: () => import('@/views/blog/ForePublish.vue')
            },
            {
                path:'/profile',
                name:'ForeProfile',
                // props:true,
                // props: { newsletterPopup: false },
                component: () => import('@/views/blog/ForeProfile.vue')
            }
        ]
    }
]

const router = createRouter({
    history: createWebHistory(),
    routes
})


router.beforeEach((to,from,next)=>{
    let userJson = sessionStorage.getItem("user");
    if(to.path === '/publish' ){
        // console.log("userJson",userJson)
        if(!userJson || userJson == null){
            next('/login');
        }else{
            next();
        }

    }else{
        next();
    }

})

export default router
